package com.juntuo.controller;

import com.github.pagehelper.PageInfo;
import com.juntuo.entity.Device;
import com.juntuo.platformUtils.PostAsynCommandV4Util;
import com.juntuo.service.DeviceHistoryService;
import com.juntuo.service.DeviceService;
import com.juntuo.util.BaiduMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;
import java.util.logging.Logger;

/**
 * 简介：
 * 作者：Jason (chenxj@hadlinks.com)
 * 时间：2018/4/8 15:51
 */
@RestController
@RequestMapping("/api")
public class ApiOutController {
    org.apache.log4j.Logger log= org.apache.log4j.Logger.getLogger(ApiOutController.class);
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private DeviceHistoryService deviceHistoryService;


    @RequestMapping(value = "/device/{deviceId}",method = RequestMethod.GET)
    public Device findDevice(@PathVariable String deviceId, String token){
        log.info("api---getDeviceByDeviceId");
        Device device = new Device();
        device.setDeviceId(deviceId);
        return deviceService.selectOne(device);
    }

    @RequestMapping(value="/page",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<Map<String,Object>> getAllList(int pageNumber, int pageSize, String parkNumber, String name) throws Exception {
        log.info("api---getAllList");
        return deviceService.findPagedDevice(pageNumber,pageSize,null,null,null,null,parkNumber,name);
    }

    /**
     *
     * @param pageNumber
     * @param pageSize
     * @param lat
     * @param lon
     * @param distance
     * @param type  0为所有车  1为空位
     * @return
     * @throws Exception
     */
    @RequestMapping(value="/nearby",method = RequestMethod.GET)
    @ResponseBody
    public PageInfo<Map<String,Object>> getNearByList(int pageNumber, int pageSize,Double lat,Double lon,Double distance,int type) throws Exception {
        log.info("api---getNearByList");
        return deviceService.selectNearByDevices(pageNumber,pageSize,lat,lon,distance,type);
    }
    @RequestMapping(value = "/device/park/{parkNumber}",method = RequestMethod.GET)
    public Device findDeviceByParkNumber(@PathVariable String parkNumber){
        log.info("api---findDeviceByParkNumber");
        Device device = new Device();
        device.setParkNumber(parkNumber);
        return deviceService.selectOne(device);
    }

    @RequestMapping(value = "/command/{deviceId}",method = RequestMethod.POST)
    public int creatCommand(@PathVariable String deviceId) throws Exception {
        PostAsynCommandV4Util.command(deviceId);
        return 1;
    }


}
